package cn.qiao.demo.aop;

import cn.qiao.demo.annotation.UniversalLog;
import cn.qiao.demo.util.AsynUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;


/**
 * 系统操作日志的切面工具
 *
 * @author callme-qiao
 */
@Aspect
@Component
public class LogAop {

    public static int a = 0;

    @Pointcut("execution(* cn.qiao.demo.controller.*.*(..))")
    public void controllerAspect() {
        System.out.println("测试");
    }

    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint) {
        System.out.println("==========执行controller前置通知===============");

    }



    /**
     * 后置通知 用于拦截Controller层记录用户的操作
     *
     * @param joinPoint 切点
     */
    @After("controllerAspect()")
    public void after(JoinPoint joinPoint) {
        try {
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            Method method = methodSignature.getMethod();
            UniversalLog annotation1 = method.getAnnotation(UniversalLog.class);
            if (annotation1!= null){
                String s = a++ +"";

            }

            Object[] args = joinPoint.getArgs();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 异常通知 用于拦截记录异常日志
     *
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(pointcut = "controllerAspect()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {

    }


}
